#!/usr/bin/env bash
#
#    Linie - Install Internet Explorer on Linux
#
#    Copyright (C) 2010 Raghu Kaippully <raghu.kb@gmail.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


# Logging functions

export LOG_ERROR=0
export LOG_WARNING=1
export LOG_INFO=2
export LOG_DEBUG=3

# Print a message if debug is on
# $* Message to be printed
function log_debug {
	[ "${LOGLEVEL}" -ge "${LOG_DEBUG}" ] && echo "DEBUG: $*"
}

# Print a message if info is on
# $* Message to be printed
function log_info {
	[ "${LOGLEVEL}" -ge "${LOG_INFO}" ] && echo "INFO: $*"
}

# Print a message if warning is on
# $* Message to be printed
function log_warning {
	[ "${LOGLEVEL}" -ge "${LOG_WARNING}" ] && echo "WARNING: $*"
}

# Print a message if error is on
# $* Message to be printed
function log_error {
	[ "${LOGLEVEL}" -ge "${LOG_INFO}" ] && echo "ERROR: $*"
}

# Print all messages from stdin if on debug mode
function debugfilter {
	while read line; do
		log_debug $line
	done
	return 0
}

# Print an error message and exit
function error_exit {
	log_error $1
	exit 1
}

# Dependencies

# Check wine installation
function check_wine {
	which wine &> /dev/null || error_exit "It seems wine is not installed"
}

# Check winetricks installation
function check_winetricks {
	which winetricks &> /dev/null || error_exit "It seems winetricks is not installed"
}

# check for wget
function check_wget {
	which wget &> /dev/null || error_exit "It seems wget is not installed"
}

# check for md5sum
function check_md5sum {
	which md5sum &> /dev/null || error_exit "It seems md5sum is not installed"
}

# Delete any existing installations as mentioned by user
function do_cleanup {
	if [ ${CLEAN_ALL} == 1 ]; then
		log_info "Removing ${BASEDIR}..."
		rm -rf ${BASEDIR}
	fi
	if [ ${CLEAN_IE7} == 1 ]; then
		log_info "Removing ${BASEDIR}/ie7..."
		rm -rf ${BASEDIR}/ie7
	fi
	if [ ${CLEAN_IE8} == 1 ]; then
		log_info "Removing ${BASEDIR}/ie8..."
		rm -rf ${BASEDIR}/ie8
	fi
}

# Download files to a standard location
# $1 is the output filename, $2 is the URL
function download {
	log_info "Downloading $1..."
	(cd ${DOWNLOADDIR}; [ -r "$1" ] || wget --continue --progress=dot:mega --output-document="$1" "$2") || error_exit "Failed to download $1"
}

# Runs a command and filters it's output using debugfilter. If the command returns with a
# non-zero status, use error_exit to exit.
#
# $1 - the error message for passing to error_exit
# $2, $3, ... - the command to run and its parameters
function error_check_and_debug {
	message=$1; shift
	(set -o pipefail; $* |& debugfilter) || error_exit $1
}
